package com.boomsecret.juc.atomic;

import java.util.concurrent.atomic.AtomicLongFieldUpdater;

/**
 * @author wangjinliang on 2019/1/17.
 */
public class AtomicLongFieldUpdaterDemo {
    public static void main(String[] args) {
        AtomicLongFieldUpdater<LongUser> atomicLongFieldUpdater = AtomicLongFieldUpdater.newUpdater(LongUser.class, "count");
        LongUser longUser = new LongUser();
        System.out.println(atomicLongFieldUpdater.compareAndSet(longUser, 0, 88));
        System.out.println(atomicLongFieldUpdater.get(longUser));

        System.out.println("-------------");
        System.out.println(atomicLongFieldUpdater.weakCompareAndSet(longUser, 0, 188));
        System.out.println(atomicLongFieldUpdater.get(longUser));

        System.out.println("-------------");
        System.out.println(atomicLongFieldUpdater.accumulateAndGet(longUser, 12, (left, right) -> left + right));
        System.out.println(atomicLongFieldUpdater.get(longUser));

        System.out.println("-------------");
        System.out.println(atomicLongFieldUpdater.updateAndGet(longUser, operand -> operand + 88));
        System.out.println(atomicLongFieldUpdater.get(longUser));
    }

    public static class LongUser {
        volatile long count;
    }
}
